package demo4.controller;

import demo4.mapper.IUserMapper;
import demo4.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Scanner;

@Component
public class UserController {
    @Autowired
    private IUserMapper userMapper;

    public void start() {
//        System.out.println("UserController：开始处理用户请求");
//        System.out.println("UserController：开始处理注册");
//        System.out.println("UserController：获取用户输入信息");
//        userMapper.insert(null);//将数据插入表中
//        System.out.println("UserController：注册成功");
        Scanner sc = new Scanner(System.in);
        System.out.println("请选择操作：1.注册 2.登录 3.展示用户列表 4.修改用户信息");
        int choice = sc.nextInt();
        switch (choice) {
            case 1:
                userReg();
                break;
            case 2:
                userLogin();
                break;
            case 3:
                userList();
                break;
            case 4:
                userUpdate();
                break;
            default:
                System.out.println("输入错误");
                break;
        }
    }

    private void userUpdate() {
        System.out.println("开始修改");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = sc.nextLine();
        User user = userMapper.selectByUsername(username);
        if (user == null) {
            System.out.println("用户不存在");
        }else {
            System.out.println("请输入密码");
            String password = sc.nextLine();
            user.setPassword(password);
            System.out.println("请输入昵称");
            String nickname = sc.nextLine();
            user.setNickname(nickname);
            System.out.println("请输入年龄");
            int age = sc.nextInt();
            user.setAge(age);
            int result = userMapper.update(user);
            System.out.println( result > 0 ? "修改成功" : "修改失败");
        }

    }

    private void userReg() {
        System.out.println("开始注册");
        User user = new User();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        user.setUsername(sc.nextLine());
        System.out.println("请输入密码：");
        user.setPassword(sc.nextLine());
        System.out.println("请输入昵称：");
        user.setNickname(sc.nextLine());
        System.out.println("请输入年龄：");
        user.setAge(sc.nextInt());
        if (userMapper.selectByUsername(user.getUsername()) == null) {
            int res = userMapper.insert(user);
            System.out.println(res > 0 ? "注册成功" : "注册失败");
        } else {
            System.out.println("用户已存在");
        }
    }

    private void userLogin() {
        System.out.println("欢迎光临");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username = sc.nextLine();
        System.out.println("请输入密码：");
        String password = sc.nextLine();
        User user = userMapper.selectByUsername(username);
//        System.out.println(user!=null?"登录成功":"登录失败");
        if (user != null) {
            if (user.getPassword().equals(password)) {
                System.out.println("登录成功");
            } else {
                System.out.println("密码错误");
            }
        } else {
            System.out.println("用户不存在");
        }
    }

    private void userList() {
        System.out.println("用户列表");
        List<User> list = userMapper.selectAll();
        for (User user : list) {
            System.out.println(user);
        }
    }
}
